package com.company.cloud.pay.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.pay.modules.cache.model.PayChannelConfigModel;
import com.company.cloud.pay.modules.mgr.controller.req.PayChannelConfigQuery4PageParam;
import com.company.cloud.pay.modules.mgr.controller.req.PayChannelConfigSaveParam;
import com.company.cloud.pay.modules.mgr.controller.req.PayChannelConfigUpdateParam;
import com.company.cloud.pay.modules.mgr.controller.req.PayChannelConfigUpdateStateParam;
import com.company.cloud.pay.modules.mgr.service.PayChannelConfigMgrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Login
@RestController
@RequestMapping("/app/pay/companyChannelConfig")
@Api(tags = "Pay_APP_公司支付通道配置服务")
public class PayCompanyChannelConfigAppController {

    @Autowired
    private PayChannelConfigMgrService payChannelConfigMgrService;

    @PostMapping("/query")
    @ApiOperation(value = "查询", response = PayChannelConfigModel.class)
    public BaseResult query(@RequestBody PayChannelConfigQuery4PageParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setCompanyNo(userInfo.getCompanyNo());
        IPage<PayChannelConfigModel> data = payChannelConfigMgrService.query(params);
        return BaseResult.ok(data);
    }

    @GetMapping("/get")
    @ApiOperation(value = "详细", response = PayChannelConfigModel.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        if (CheckUtils.isEmpty(id)) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PayChannelConfigModel data = payChannelConfigMgrService.get(id, true, userInfo);
        return BaseResult.ok(data);
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增", response = PayChannelConfigModel.class)
    public BaseResult save(@RequestBody PayChannelConfigSaveParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        params.setCompanyNo(userInfo.getCompanyNo());
        PayChannelConfigModel data = payChannelConfigMgrService.save(params, userInfo);
        return BaseResult.ok(data);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改", response = PayChannelConfigModel.class)
    public BaseResult update(@RequestBody PayChannelConfigUpdateParam params) {
        ValidatorUtils.validate(params);
        params.setCompany(true);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        PayChannelConfigModel data = payChannelConfigMgrService.update(params, userInfo);
        return BaseResult.ok(data);
    }


    @PostMapping("/updateState")
    @ApiOperation(value = "修改状态", response = BaseResult.class)
    public BaseResult updateState(@RequestBody PayChannelConfigUpdateStateParam params) {
        ValidatorUtils.validate(params);
        params.setCompany(true);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        payChannelConfigMgrService.updateState(params, userInfo);
        return BaseResult.ok();
    }


    @GetMapping("/delete")
    @ApiOperation(value = "删除")
    public BaseResult delete(@RequestParam("id") Integer id) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        payChannelConfigMgrService.del(id, true, userInfo);
        return BaseResult.ok();
    }


}
